extends Resource
class_name Item

# 物品基本信息
@export var item_name: String = "未知物品"
@export var description: String = "一个神秘的物品。"
@export var icon: Texture2D = null
@export var stack_size: int = 1  # 当前堆叠数量
@export var max_stack: int = 20   # 最大堆叠数量

# 物品类型
enum ItemType {
	WEAPON,      # 武器
	ARMOR,       # 防具
	ACCESSORY,   # 饰品
	CONSUMABLE,  # 消耗品
	MATERIAL,    # 材料
	QUEST,       # 任务物品
	SPIRIT_STONE,# 灵石(货币)
	SPIRIT_HERB, # 灵草
	TREASURE     # 宝物
}

@export var item_type: int = ItemType.MATERIAL

# 物品品质
enum ItemQuality {
	COMMON,     # 普通
	UNCOMMON,   # 优秀
	RARE,       # 稀有
	EPIC,       # 史诗
	LEGENDARY,  # 传说
	MYTHIC      # 神话
}

@export var quality: int = ItemQuality.COMMON

# 物品价值（灵石）
@export var value: int = 1

# 物品等级要求
@export var required_level: int = 1

# 是否可以装备
@export var can_equip: bool = false

# 装备槽位
enum EquipSlot {
	WEAPON,    # 武器
	HEAD,      # 头部
	BODY,      # 身体
	HANDS,     # 手部
	FEET,      # 脚部
	ACCESSORY1,# 饰品1
	ACCESSORY2 # 饰品2
}

@export var equip_slot: int = EquipSlot.WEAPON

# 装备属性加成
@export var health_bonus: int = 0
@export var spirit_bonus: int = 0
@export var attack_bonus: int = 0
@export var defense_bonus: int = 0
@export var speed_bonus: int = 0

# 消耗品效果
@export var health_restore: int = 0
@export var spirit_restore: int = 0
@export var effect_duration: int = 0  # 效果持续回合数（战斗中）

# 信号
signal item_used(target)

# 使用物品
func use(target) -> bool:
	if item_type == ItemType.CONSUMABLE:
		# 应用消耗品效果
		if target.has_method("heal") and health_restore > 0:
			target.heal(health_restore)
		
		if target.has_method("recover_spirit") and spirit_restore > 0:
			target.recover_spirit(spirit_restore)
		
		# 减少堆叠数量
		stack_size -= 1
		
		emit_signal("item_used", target)
		return true
	
	return false

# 获取物品品质颜色
func get_quality_color() -> Color:
	match quality:
		ItemQuality.COMMON:
			return Color(0.8, 0.8, 0.8)  # 灰白色
		ItemQuality.UNCOMMON:
			return Color(0.0, 0.8, 0.0)  # 绿色
		ItemQuality.RARE:
			return Color(0.0, 0.5, 1.0)  # 蓝色
		ItemQuality.EPIC:
			return Color(0.7, 0.0, 1.0)  # 紫色
		ItemQuality.LEGENDARY:
			return Color(1.0, 0.5, 0.0)  # 橙色
		ItemQuality.MYTHIC:
			return Color(1.0, 0.0, 0.0)  # 红色
		_:
			return Color(1.0, 1.0, 1.0)  # 白色

# 获取物品类型名称
func get_type_name() -> String:
	match item_type:
		ItemType.WEAPON:
			return "武器"
		ItemType.ARMOR:
			return "防具"
		ItemType.ACCESSORY:
			return "饰品"
		ItemType.CONSUMABLE:
			return "消耗品"
		ItemType.MATERIAL:
			return "材料"
		ItemType.QUEST:
			return "任务物品"
		ItemType.SPIRIT_STONE:
			return "灵石"
		ItemType.SPIRIT_HERB:
			return "灵草"
		ItemType.TREASURE:
			return "宝物"
		_:
			return "未知"

# 获取装备槽位名称
func get_equip_slot_name() -> String:
	if not can_equip:
		return ""
	
	match equip_slot:
		EquipSlot.WEAPON:
			return "武器"
		EquipSlot.HEAD:
			return "头部"
		EquipSlot.BODY:
			return "身体"
		EquipSlot.HANDS:
			return "手部"
		EquipSlot.FEET:
			return "脚部"
		EquipSlot.ACCESSORY1:
			return "饰品1"
		EquipSlot.ACCESSORY2:
			return "饰品2"
		_:
			return ""

# 获取物品详细描述
func get_detailed_description() -> String:
	var desc = description + "\n"
	
	if can_equip:
		desc += "装备类型: " + get_equip_slot_name() + "\n"
		
		# 添加属性加成信息
		var has_bonus = false
		
		if health_bonus != 0:
			desc += "生命值: %+d\n" % health_bonus
			has_bonus = true
		
		if spirit_bonus != 0:
			desc += "真气值: %+d\n" % spirit_bonus
			has_bonus = true
		
		if attack_bonus != 0:
			desc += "攻击力: %+d\n" % attack_bonus
			has_bonus = true
		
		if defense_bonus != 0:
			desc += "防御力: %+d\n" % defense_bonus
			has_bonus = true
		
		if speed_bonus != 0:
			desc += "速度: %+d\n" % speed_bonus
			has_bonus = true
		
		if not has_bonus:
			desc += "无属性加成\n"
	
	elif item_type == ItemType.CONSUMABLE:
		var has_effect = false
		
		if health_restore > 0:
			desc += "恢复生命值: %d\n" % health_restore
			has_effect = true
		
		if spirit_restore > 0:
			desc += "恢复真气值: %d\n" % spirit_restore
			has_effect = true
		
		if effect_duration > 0:
			desc += "效果持续: %d回合\n" % effect_duration
			has_effect = true
		
		if not has_effect:
			desc += "无特殊效果\n"
	
	desc += "品质: " + ["普通", "优秀", "稀有", "史诗", "传说", "神话"][quality] + "\n"
	return desc
